package com.summer.study.leetcode.thread.h2o_116;

import java.util.concurrent.Semaphore;

/**
 * @auther: 杨杰
 * @date: 2019/7/25 14:29
 * @description:
 */
public class H2ODemo {

    private Semaphore h = new Semaphore(2);
    private Semaphore o = new Semaphore(1);


    public void hydrogen(Runnable releaseHydrogen) throws InterruptedException {
        h.acquire();
        // releaseHydrogen.run() outputs "H". Do not change or remove this line.
        releaseHydrogen.run();
        if (h.availablePermits() == 0) {
            o.release();
        }
    }

    public void oxygen(Runnable releaseOxygen) throws InterruptedException {
        o.acquire();
        // releaseOxygen.run() outputs "H". Do not change or remove this line.
        releaseOxygen.run();
        h.release(2);
    }

}
